<template>
  <div class="item" style="width: 100%">
    <div class="name">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown class="dropdown" @command="commandEvent">
      <span>
        <span>操作</span>
        <i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add"> 添加子部门 </el-dropdown-item>
          <el-dropdown-item v-if="!item.top" command="look">
            查看部门
          </el-dropdown-item>
          <el-dropdown-item v-if="!item.top" command="del">
            删除部门
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companydepartmentDel } from '@/api/departments.js'
export default {
  name: '',
  props: {
    item: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {}
  },
  computed: {},
  created() {},
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          // 触发bus的传值,将当前点击项数据传递给弹窗(add)组件
          this.$bus.$emit('showDialog', true, this.item, 'add')
          console.log('添加')
          break
        case 'look':
          this.$bus.$emit('showDialog', true, this.item, 'edit')
          console.log('查看')
          break
        case 'del':
          console.log('删除')
          this.$confirm('你确定要删除该部门吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async() => {
              await companydepartmentDel(this.item.id)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.$emit('getData')
            })
            .catch(() => {})
          // this.$confirm('你确定要删除该部门吗？', '提示')
          //   .then(async() => {
          // 调用删除api
          //     await companydepartmentDel(this.item.id)
          // 提示用户删除成功
          //     this.$message.success('删除成功')
          // 刷新页面
          //     this.$emit('getData')
          //   })
          //   .catch(() => {})
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.item {
  display: flex;
  align-items: center;
  .name {
    flex: 1;
  }
  .manager {
    width: 120px;
    margin-right: 20px;
    text-align: right;
  }
}
</style>
